﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace CalemShop.Classes
{
    public delegate void UpdateErrorConsoleCallback(string text);

    public static class Reporter
    {
        private static ArrayList errorList = new ArrayList();
        private static UpdateErrorConsoleCallback errorCallback = null;

        public static UpdateErrorConsoleCallback ErrorConsoleCallback
        {
            set
            {
                errorCallback = value;
            }
        }

        public static void WriteError(string message)
        {
            errorList.Add(message);
            if (errorCallback != null)
            {
                errorCallback(message);
            }
        }

        public static void WriteError(Exception throwable)
        {
            errorList.Add(throwable.Message);
            if (errorCallback != null)
            {
                errorCallback(throwable.Message);
            }
        }

        public static void WriteErrorEx(Exception throwable)
        {
            errorList.Add(throwable.Message + Environment.NewLine + throwable.StackTrace);
            if (errorCallback != null)
            {
                errorCallback(throwable.Message + Environment.NewLine + throwable.StackTrace);
            }
        }

        public static StringBuilder ErrorLogData
        {
            get
            {
                StringBuilder builder = new StringBuilder();
                for (int i = errorList.Count; --i >= 0; )
                {
                    builder.AppendFormat("{0}", errorList[i]).AppendLine();
                }

                return builder;
            }
        }
    }
}
